package com.aduerr.v1shouyin.util;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import com.aduerr.v1shouyin.App;

import java.util.ArrayList;
import java.util.List;


public class CommonData {

    public static final String WECHATID = "wx4ccefd8d9db8f0cb";
    public static final List<Activity> ACTIVITYS = new ArrayList<>();
    public static Context appContext;
    private static String mDeviceId;
    private static String mUmengDeviceId;
    private static String mSiteUserId;
    private static String mSiteId;
    private static String mLogId;
    private static String mPass;
    private static int mWeixinpay = -1;
    private static int mAlipay = -1;
    private static int mBaidupay = -1;
    private static String mDisplayName;
    private static String mLevel;
    private static String mShopName;
    private static String mAddress;
    private static String mPhone;
    private static String mAlipayAccount;
    private static String mWeiXinAccount;
    private static String mCompany;

    public static void addActivity(Activity activity) {
        if(ACTIVITYS.contains(activity))
            return;
        ACTIVITYS.add(
                activity
        );
    }

    public static void finishOtherActivity(Activity activity) {
        for(int i = 0; i < ACTIVITYS.size(); i++) {
            if(ACTIVITYS.get(i) == activity)
                continue;
            ACTIVITYS.remove(i).finish();
        }
    }

    public static void finishActivity() {
        for(int i = 0; i < ACTIVITYS.size(); i++) {
            ACTIVITYS.remove(i).finish();
        }
    }

    public static String deviceId() {
        if(mDeviceId == null) {
            mDeviceId = "android_" + Installation.id(CommonData.appContext);
        }
        return mDeviceId;
    }

    public static void saveShowHolle(boolean holle) {
        SharedPreferences sp = CommonData.appContext.getSharedPreferences("SP",
                Context.MODE_PRIVATE);
        Editor editor = sp.edit();
        editor.putBoolean("holle", holle);
        editor.apply();
    }

    public static boolean isShowHolle() {
        SharedPreferences sp = CommonData.appContext.getSharedPreferences("SP",
                Context.MODE_PRIVATE);
        return sp.getBoolean("holle", false);
    }

    public static String umengDeviceId() {
        if(mUmengDeviceId == null) {
            mUmengDeviceId = "android_"
                    + android.provider.Settings.Secure.getString(
                    CommonData.appContext.getContentResolver(),
                    android.provider.Settings.Secure.ANDROID_ID);
        }
        return mUmengDeviceId;
    }

    public static String shebeiId() {
        if(mUmengDeviceId == null) {
            mUmengDeviceId = "V1_"
                    + android.provider.Settings.Secure.getString(
                    App.getApp().getContentResolver(),
                    android.provider.Settings.Secure.ANDROID_ID);
        }
        return mUmengDeviceId;
    }

    private static boolean isNotEmpty(String str) {
        return str != null && !str.isEmpty();
    }

    public static String getSiteUserId() {
        if(isNotEmpty(mSiteUserId)) {
            return mSiteUserId;
        }
        mSiteUserId = getString("siteuserid");
        return mSiteUserId;
    }

    public static void setSiteUserId(String value) {
        mSiteUserId = value;
        saveString("siteuserid", value);
    }

    public static String getLogId() {
        if(isNotEmpty(mLogId)) {
            return mLogId;
        }
        mLogId = getString("logid" + Utils.getVersionName(appContext));
        return mLogId;
    }

    public static void setLogId(String value) {
        mSiteUserId = value;
        saveString("logid" + Utils.getVersionName(appContext), value);
    }

    public static String getPass() {
        if(isNotEmpty(mPass)) {
            return mPass;
        }
        mPass = getString("pass");
        return mPass;
    }

    public static void setPass(String value) {
        mPass = value;
        saveString("pass", value);
    }

    public static String getSiteId() {
        if(isNotEmpty(mSiteId)) {
            return mSiteId;
        }
        mSiteId = getString("siteid");
        return mSiteId;
    }

    public static void setSiteId(String value) {
        mSiteId = value;
        saveString("siteid", value);
    }

    public static void setWeixinAccount(String value) {
        mWeiXinAccount = value;
        saveString("mWeiXinAccount", value);
    }

    public static String getWeixiAccount() {
        if(mWeiXinAccount != null) {
            return mWeiXinAccount;
        }
        mWeiXinAccount = getString("mWeiXinAccount", "未设置");
        return mWeiXinAccount;
    }

    public static int getWeixinpay() {
        if(mWeixinpay != -1) {
            return mWeixinpay;
        }
        mWeixinpay = getInt("weixinpay");
        return mWeixinpay;
    }

    public static void setWeixinpay(int value) {
        mWeixinpay = value;
        saveInt("weixinpay", value);
    }

    public static int getAlipay() {
        if(mAlipay != -1) {
            return mAlipay;
        }
        mAlipay = getInt("alipay");
        return mAlipay;
    }

    public static void setAlipay(int value) {
        mAlipay = value;
        saveInt("alipay", value);
    }

    public static int getBaidupay() {
        if(mBaidupay != -1) {
            return mBaidupay;
        }
        mBaidupay = getInt("baidupay");
        return mBaidupay;
    }

    public static void setBaidupay(int value) {
        mBaidupay = value;
        saveInt("baidupay", value);
    }

    public static String getDisplayName() {
        if(isNotEmpty(mDisplayName)) {
            return mDisplayName;
        }
        mDisplayName = getString("displayname");
        return mDisplayName;
    }

    public static void setDisplayName(String value) {
        mDisplayName = value;
        saveString("displayname", value);
    }

    public static String getShopName() {
        if(isNotEmpty(mShopName)) {
            return mShopName;
        }
        mShopName = getString("shopname");
        return mShopName;
    }

    public static void setShopName(String value) {
        mShopName = value;
        saveString("shopname", value);
    }

    public static String getPhone() {
        if(isNotEmpty(mPhone)) {
            return mPhone;
        }
        mPhone = getString("phone");
        return mPhone;
    }

    public static void setPhone(String value) {
        mPhone = value;
        saveString("phone", value);
    }

    public static String getLevel() {
        if(isNotEmpty(mLevel)) {
            return mLevel;
        }
        mLevel = getString("level");
        return mLevel;
    }

    public static void setLevel(String value) {
        mLevel = value;
        saveString("level", value);
    }

    public static String getAlipayAccount() {
        if(isNotEmpty(mAlipayAccount)) {
            return mAlipayAccount;
        }
        mAlipayAccount = getString("alipayaccount", "未设置");
        return mAlipayAccount;
    }

    public static void setAlipayAccount(String value) {
        mAlipayAccount = value;
        saveString("alipayaccount", value);
    }

    public static String getCompany() {
        if(isNotEmpty(mCompany)) {
            return mCompany;
        }
        mCompany = getString("company");
        return mCompany;
    }

    public static void setCompany(String value) {
        mCompany = value;
        saveString("company", value);
    }

    public static String getAddress() {
        if(isNotEmpty(mAddress)) {
            return mAddress;
        }
        mAddress = getString("address");
        return mAddress;
    }

    public static void setAddress(String value) {
        mAddress = value;
        saveString("address", value);
    }

    public static String getString(String name, String def) {
        SharedPreferences sp = CommonData.appContext.getSharedPreferences("SP",
                Context.MODE_PRIVATE);
        return sp.getString(name, def);
    }

    public static String getString(String name) {
        SharedPreferences sp = CommonData.appContext.getSharedPreferences("SP",
                Context.MODE_PRIVATE);
        return sp.getString(name, "");
    }

    public static int getInt(String name) {
        SharedPreferences sp = CommonData.appContext.getSharedPreferences("SP",
                Context.MODE_PRIVATE);
        return sp.getInt(name, -1);
    }

    public static int getInt(String name, int def) {
        SharedPreferences sp = CommonData.appContext.getSharedPreferences("SP",
                Context.MODE_PRIVATE);
        return sp.getInt(name, def);
    }

    public static void clear() {
        SharedPreferences sp = CommonData.appContext.getSharedPreferences("SP",
                Context.MODE_PRIVATE);
        Editor editor = sp.edit();
        mLogId = null;
        editor.clear();
        editor.apply();
    }

    public static void saveString(String name, String value) {
        SharedPreferences sp = CommonData.appContext.getSharedPreferences("SP",
                Context.MODE_PRIVATE);
        Editor editor = sp.edit();
        editor.putString(name, value);
        editor.apply();
    }

    public static void saveInt(String name, int value) {
        SharedPreferences sp = CommonData.appContext.getSharedPreferences("SP",
                Context.MODE_PRIVATE);
        Editor editor = sp.edit();
        editor.putInt(name, value);
        editor.apply();
    }

    @Override
    protected void finalize() throws Throwable {
        super.finalize();
        ACTIVITYS.clear();
    }

}
